package com.ruoyi.web.controller.train;

import java.time.LocalTime;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GfTrain;
import com.ruoyi.system.service.IGfTrainService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 动车信息Controller
 * 
 * @author ruoyi
 * @date 2022-06-16
 */
@RestController
@RequestMapping("/system/train")
public class GfTrainController extends BaseController
{
    @Autowired
    private IGfTrainService gfTrainService;

    /**
     * 查询动车信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:train:list')")
    @GetMapping("/list")
    public TableDataInfo list(GfTrain gfTrain)
    {
        startPage();
        List<GfTrain> list = gfTrainService.selectGfTrainList(gfTrain);
        return getDataTable(list);
    }

    /**
     * 导出动车信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:train:export')")
    @Log(title = "动车信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GfTrain gfTrain)
    {
        List<GfTrain> list = gfTrainService.selectGfTrainList(gfTrain);
        ExcelUtil<GfTrain> util = new ExcelUtil<GfTrain>(GfTrain.class);
        util.exportExcel(response, list, "动车信息数据");
    }

    /**
     * 获取动车信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:train:query')")
    @GetMapping(value = "/{trainId}")
    public AjaxResult getInfo(@PathVariable("trainId") Long trainId)
    {
        return AjaxResult.success(gfTrainService.selectGfTrainByTrainId(trainId));
    }

    /**
     * 新增动车信息
     */
    @PreAuthorize("@ss.hasPermi('system:train:add')")
    @Log(title = "动车信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GfTrain gfTrain)
    {
        //新增信息发车时间规则
        LocalTime trainStartTime = gfTrain.getTrainStartTime();
        LocalTime trainEndTime = gfTrain.getTrainEndTime();
        if (trainStartTime.isAfter(trainEndTime)) {
            return error("发车时间不能晚于到达时间!!");
        }
        return toAjax(gfTrainService.insertGfTrain(gfTrain));
    }

    /**
     * 修改动车信息
     */
    @PreAuthorize("@ss.hasPermi('system:train:edit')")
    @Log(title = "动车信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GfTrain gfTrain)
    {
        //修改信息发车时间规则
        LocalTime trainStartTime = gfTrain.getTrainStartTime();
        LocalTime trainEndTime = gfTrain.getTrainEndTime();
        if (trainStartTime.isAfter(trainEndTime)) {
            return error("发车时间不能晚于到达时间!!");
        }
        return toAjax(gfTrainService.updateGfTrain(gfTrain));
    }

    /**
     * 删除动车信息
     */
    @PreAuthorize("@ss.hasPermi('system:train:remove')")
    @Log(title = "动车信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{trainIds}")
    public AjaxResult remove(@PathVariable Long[] trainIds)
    {
        return toAjax(gfTrainService.deleteGfTrainByTrainIds(trainIds));
    }
}
